package org.example;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.xml.bind.Element;
import java.io.File;
import java.io.IOException;
import java.util.List;

import static java.lang.Thread.sleep;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 晓星航
 * Date: 2024-01-23
 * Time: 21:23
 */
public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
//        test01();
//        test02();
//        test05();
//        test06();
//        test07();
        test08();
    }

    private static void test08() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("D://20230521jietu.png"));

    }

    private static void test07() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).click();

    }

    private static void test06() {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //判断元素是否可以被点击
        WebDriverWait wait = new WebDriverWait(webDriver,3000);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#tsn_inner > div:nth-child(2) > span")));
    }

    private static void test05() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度首页
        webDriver.get("http://www.baidu.com/");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
        if (url.equals("http://www.baidu.com/") && title.equals("百度一下，你就知道")) {
            System.out.println("当前页面url：" + url + "，当前页面title：" + title);
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
    }

    private static void test02() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        sleep(3000);
        //打开百度首页
        webDriver.get("http://www.baidu.com");
        //找到百度搜索输入框，输入“英雄联盟”
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("英雄联盟");
        //点击了百度一下按钮
        webDriver.findElement(By.cssSelector("#su")).submit();
        sleep(3000);
        //清空百度搜索输入框中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    private static void test01() throws InterruptedException{
        int flag = 0;
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度首页
        webDriver.get("http://www.baidu.com/");
        //找到百度搜索输入框
//        WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //输入软件测试
        element.sendKeys("软件测试");
        //找到百度一下按钮
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //校验
        //找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i < elements.size(); i++) {
//            System.out.println(elements.get(i).getText());
            //如果返回的结果包含有软件测试，证明测试通过，否则测试不通过
            if (elements.get(i).getText().equals("测试")){
                flag = 1;
                System.out.println("测试通过");
                break;
            }
        }
        if (flag == 0) {
            System.out.println("测试不通过");
        }
    }
}